package com.heima.wemedia.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.consatnts.message.NewsAutoScanConstants;
import com.heima.common.consatnts.message.NewsUpOrDownConstants;
import com.heima.common.consatnts.message.PublishArticleConstants;
import com.heima.common.consatnts.wemedia.WemediaConstants;
import com.heima.common.exception.CustException;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.WmThreadLocalUtils;
import com.heima.model.wemedia.dtos.NewsAuthDTO;
import com.heima.model.wemedia.dtos.WmNewsDTO;
import com.heima.model.wemedia.dtos.WmNewsPageReqDTO;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmNewsMaterial;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.model.wemedia.vos.WmNewsVO;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.mapper.WmNewsMaterialMapper;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmNewsService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * author by itheima
 *
 * @Date 2022/3/2
 * Description
 */
@Service
@Slf4j
public class WmNewsServiceImpl extends ServiceImpl<WmNewsMapper, WmNews> implements WmNewsService {
    @Autowired
    private WmNewsMaterialMapper wmNewsMaterialMapper;
    @Autowired
    WmMaterialMapper wmMaterialMapper;
    @Value("${file.oss.web-site}")
    String webSite;

    /**
     * 分页带条件查询自媒体文章列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findList(WmNewsPageReqDTO dto) {
        //校验参数
        if (dto == null) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID, "参数不能为空");
        }
        //检查参数
        dto.checkParam();
        WmUser user = WmThreadLocalUtils.getUser();
        if (user == null) {
            CustException.cust(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }
        LambdaQueryWrapper<WmNews> wrapper = new LambdaQueryWrapper<>();
        //文章标题模糊查询
        wrapper.like(StringUtils.isNotBlank(dto.getKeyword()), WmNews::getTitle, dto.getKeyword());
        //按照屏幕ID查询
        wrapper.eq(dto.getChannelId() != null, WmNews::getChannelId, dto.getChannelId());
        wrapper.eq(dto.getStatus() != null, WmNews::getStatus, dto.getStatus());
        //如果开始时间，结束时间不为空按照时间区间查询
        wrapper.ge(dto.getBeginPubDate() != null, WmNews::getPublishTime, dto.getBeginPubDate());
        wrapper.le(dto.getEndPubDate() != null, WmNews::getSubmitedTime, dto.getEndPubDate());
        wrapper.eq(WmNews::getUserId, user.getId());
        Page<WmNews> resultPage = new Page<>(dto.getPage(), dto.getSize());
        IPage<WmNews> page = this.page(resultPage, wrapper);
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), page.getTotal());
        pageResponseResult.setData(page.getRecords());
        pageResponseResult.setHost(webSite);
        return pageResponseResult;
    }

    /**
     * * 该功能为保存、修改（是否有id）、保存草稿的共用方法
     * * 如果有id修改文章，先删除所有素材关联关系
     * * 如果没有id，保存文章
     *
     * @param dto
     * @return
     */
    @Autowired
    RabbitTemplate rabbitTemplate;

    //自媒体文章发布
    @Transactional(rollbackFor = Exception.class)
    @Override

    public ResponseResult submitNews(WmNewsDTO dto) {
        //1.参数校验 dto( 标题 content)  是否登录
        if (StringUtils.isBlank(dto.getContent()) || StringUtils.isBlank(dto.getTitle())) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID, "标题或内容不能为空");
        }
        WmUser user = WmThreadLocalUtils.getUser();
        if (user == null) {
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }
        //将dto参数里面的值设置到wmNews
        // dto ==> wmNews
        WmNews wmNews = new WmNews();
        BeanUtils.copyProperties(dto, wmNews);
        //type   -1
        if (WemediaConstants.WM_NEWS_TYPE_AUTO.equals(dto.getType())) {
            //该字段是无符号字段，存入负数会报错 ，暂时设置为null
            wmNews.setType(null);
        }
        //images 前端集合 == 》 字符串
        wmNews.setImages(imagesToStr(dto.getImages()));
        //设置用户ID
        wmNews.setUserId(user.getId());
        //2.保存  或 修改文章 (根据id)
        saveOrUpdateWmNews(wmNews);

        //如果是草稿 直接返回一个结果  不需要做处理
        if (WemediaConstants.WM_NEWS_DRAFT_STATUS.equals(dto.getStatus())) {
            return ResponseResult.okResult();
        }
        //3.保存文章内容  封面  和素材的关联关系
        //3.1抽取出内容中引用的素材url路径
        List<String> materialUrlList = parseContentImages(dto.getContent());
        //3.2保存内容和素材的关联关系
        if (!CollectionUtils.isEmpty(materialUrlList)) {
            saveRelativeInfoForContent(materialUrlList, wmNews);
        }
        //3.3保存封面和素材的关联关系
        savePelativeInfoForCover(materialUrlList, wmNews, dto);
        // 如果type == -1 需要根据内容素材列表生成封面图片
        //发送及消费消息
        rabbitTemplate.convertAndSend(NewsAutoScanConstants.WM_NEWS_AUTO_SCAN_QUEUE, wmNews.getId());
        log.info("成功发送  待审核消息==》 队列：{}， 文章id：", NewsAutoScanConstants.WM_NEWS_AUTO_SCAN_QUEUE, wmNews.getId());

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult findWmNewsById(Integer id) {
        if (id == null) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmNews wmNews = this.getById(id);
        if (wmNews == null) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        ResponseResult responseResult = ResponseResult.okResult(wmNews);
        responseResult.setHost(webSite);
        return responseResult;
    }

    //删除
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult delNews(Integer id) {
        if (id == null) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID, "文章ID不能缺少");
        }
        WmNews wmNews = this.getById(id);
        if (wmNews == null) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST, "该数据不存在");
        }
        //判断当前文章的状态 status==9 enable==1
        if (wmNews.getStatus().equals(WmNews.Status.PUBLISHED.getCode())
                && wmNews.getEnable().equals(WemediaConstants.WM_NEWS_UP)
        ) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST, "文章已发布，不能删除");
        }
        //去除素材与文章的关系
        wmNewsMaterialMapper.delete(Wrappers.<WmNewsMaterial>lambdaQuery().eq(WmNewsMaterial::getNewsId, wmNews.getId()));
        //删除文章
        removeById(wmNews.getId());
        return ResponseResult.okResult();
    }

    //上架下架
    @Override
    public ResponseResult downOrUp(WmNewsDTO dto) {
        //1.检查参数
        if(dto == null || dto.getId() == null){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        Short enable = dto.getEnable();
        if(enable == null ||
                (!WemediaConstants.WM_NEWS_UP.equals(enable)&&!WemediaConstants.WM_NEWS_DOWN.equals(enable))){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"上下架状态错误");
        }
        //2.查询文章
        WmNews wmNews = getById(dto.getId());
        if(wmNews == null){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"文章不存在");
        }
        //3.判断文章是否发布
        if(!wmNews.getStatus().equals(WmNews.Status.PUBLISHED.getCode())){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"当前文章不是发布状态，不能上下架");
        }
        //4.修改文章状态，同步到app端（后期做）TODO
        update(Wrappers.<WmNews>lambdaUpdate().eq(WmNews::getId,dto.getId())
                .set(WmNews::getEnable,dto.getEnable()));

        if(wmNews.getArticleId()!=null){
            if (WemediaConstants.WM_NEWS_UP.equals(dto.getEnable())) {
                // 上架
                rabbitTemplate.convertAndSend(
                        NewsUpOrDownConstants.NEWS_UP_OR_DOWN_EXCHANGE,
                        NewsUpOrDownConstants.NEWS_UP_ROUTE_KEY,
                        wmNews.getArticleId()
                );
                log.info(" 文章上架消息  成功发送  文章id:{}",wmNews.getArticleId());
            }else {
                // 下架
                rabbitTemplate.convertAndSend(
                        NewsUpOrDownConstants.NEWS_UP_OR_DOWN_EXCHANGE,
                        NewsUpOrDownConstants.NEWS_DOWN_ROUTE_KEY,
                        wmNews.getArticleId()
                );
                log.info(" 文章下架消息  成功发送  文章id:{}",wmNews.getArticleId());
            }
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Autowired
    WmNewsMapper wmNewsMapper;

    @Override
    public ResponseResult findAuthList(NewsAuthDTO dto) {
        //1.检查参数
        dto.checkParam();
        //记录当前页
        Integer currentPage = dto.getPage();
        //设置起始页
        dto.setPage((currentPage - 1) * dto.getSize());
        if (StringUtils.isNotBlank(dto.getTitle())) {
            dto.setTitle("%" + dto.getTitle() + "%");
        }
        //2.分页查询
        List<WmNewsVO> wmNewsVOList = wmNewsMapper.findListAndPage(dto);
        //统计多少条数据
        long count = wmNewsMapper.findListCount(dto);
        //结果返回
        PageResponseResult pageResponseResult = new PageResponseResult(currentPage, dto.getSize(), count, wmNewsVOList);
        pageResponseResult.setHost(webSite);
        return pageResponseResult;
    }

    /**
     * 查看文章详情
     *
     * @param id
     * @return
     */
    @Autowired
    WmUserMapper wmUserMapper;

    @Override
    public ResponseResult findWmNewsVo(Integer id) {
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查询文章信息
        WmNews wmNews = this.getById(id);
        if (wmNews == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //3.查询作者
        WmUser wmUser = null;
        if (wmNews.getUserId() != null) {
            wmUser = wmUserMapper.selectById(wmNews.getUserId());
        }
        //封装vo信息返回
        WmNewsVO wmNewsVO = new WmNewsVO();
        BeanUtils.copyProperties(wmNews, wmNewsVO);
        if (wmUser != null) {
            wmNewsVO.setAuthorName(wmUser.getName());

        }
        ResponseResult responseResult = ResponseResult.okResult(wmNewsVO);
        responseResult.setHost(webSite);
        return responseResult;
    }

    /**
     * 自媒体文章人工审核
     *
     * @param status 2  审核失败  4 审核成功
     * @param dto
     * @return
     */
    @Override
    public ResponseResult updateStatus(Short status, NewsAuthDTO dto) {
        if (dto == null || dto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.查询文章
        WmNews wmNews = this.getById(dto.getId());
        if (wmNews == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //检查文章状态  不能为9   已发布
        if (wmNews.getStatus().equals(WmNews.Status.PUBLISHED.getCode())) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW);
        }
        wmNews.setStatus(status);
        if (StringUtils.isNotBlank(dto.getMsg())) {
            wmNews.setReason(dto.getMsg());
        }
        updateById(wmNews);
        //TODO 通知定时发布文章
        //5. 通知定时发布文章
        // 发布时间
        long publishTime = wmNews.getPublishTime().getTime();
        // 当前时间
        long nowTime = new Date().getTime();
        long remainTime = publishTime - nowTime;
        // 发布文章
        rabbitTemplate.convertAndSend(PublishArticleConstants.DELAY_DIRECT_EXCHANGE
                , PublishArticleConstants.PUBLISH_ARTICLE_ROUTE_KEY
                , wmNews.getId()
                , (message) -> {                              // 延时消息 必设置
                    message.getMessageProperties().setHeader("x-delay", remainTime <= 0 ? 0 : remainTime);
                    return message;
                }
        );
        log.info("立即发布文章通知成功发送，文章id : {}", wmNews.getId());
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 保存 封面和素材的关联关系
     *
     * @param materialUrlList
     * @param wmNews
     * @param dto
     */
    private void savePelativeInfoForCover(List<String> materialUrlList, WmNews wmNews, WmNewsDTO dto) {
        //原本的封面集合列表
        List<String> images = dto.getImages();
        //判断类型是否为-1
        if (WemediaConstants.WM_NEWS_TYPE_AUTO.equals(dto.getType())) {
            //需要自动生成封面
            int size = materialUrlList.size();
            //如果size 数量大于0等于2 设置为多图  ， 截取素材url集合中的 1个作为封面
            if (size > 0 && size <= 2) {
                images = materialUrlList.stream().limit(1).collect(Collectors.toList());
                wmNews.setType(WemediaConstants.WM_NEWS_SINGLE_IMAGE);
            } else if (size > 2) {
                //如果 size 数量大于2  设置为多图 ， 截取素材url集合的 3 个作为封面
                images = materialUrlList.stream().limit(3).collect(Collectors.toList());
                wmNews.setType(WemediaConstants.WM_NEWS_MANY_IMAGE);
            } else {
                //设置为无图
                wmNews.setType(WemediaConstants.WM_NEWS_NONE_IMAGE);
            }
            //如果有封面图  将图片集合转为字符串设置为wmNews.images属性中
            if (!CollectionUtils.isEmpty(images)) {
                wmNews.setImages(imagesToStr(images));
            }
            //修改wmNews
            updateById(wmNews);
        }
        //如果不为空  保存关联关系
        if (!CollectionUtils.isEmpty(images)) {
            images = images.stream().map(url -> url.replaceAll(webSite, ""))
                    .collect(Collectors.toList());
            saveRelativeInfo(images, wmNews, WemediaConstants.WM_IMAGE_REFERENCE);
        }
    }

    /**
     * 保存  内容和素材的关联关系  文章内容涉及的图片 和 素材的关联关系
     *
     * @param materialUrlList
     * @param wmNews
     */
    private void saveRelativeInfoForContent(List<String> materialUrlList, WmNews wmNews) {

        saveRelativeInfo(materialUrlList, wmNews, WemediaConstants.WM_CONTENT_REFERENCE);
    }

    /**
     * 保存关联关系
     *
     * @param materialUrlList
     * @param wmNews
     * @param type
     */
    private void saveRelativeInfo(List<String> materialUrlList, WmNews wmNews, Short type) {
        //1.根据url路径列表  查询对应的id
//      List<WmMaterial> wmMaterials=  wmMaterialMapper.selectList(Wrappers.<WmMaterial>lambdaQuery()
//            .in(WmMaterial::getUrl,materialUrlList)
//                .eq(WmMaterial::getUserId,WmThreadLocalUtils.getUser().getId())
//                .select(WmMaterial::getId)
//        );
        List<Integer> ids = wmMaterialMapper.selectRelationsIds(materialUrlList, WmThreadLocalUtils.getUser().getId());
        //判断是否缺失素材
        if (CollectionUtils.isEmpty(ids) || ids.size() < materialUrlList.size()) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST, "引用的素材不存在");
        }
        //3.根据素材id 集合 文章id 引用类型  保存关联关系
//        for (WmMaterial wmMaterial : wmMaterials) {
//            WmNewsMaterial wmNewsMaterial = new WmNewsMaterial();
//            wmNewsMaterial.setMaterialId(wmMaterial.getId());
//            wmNewsMaterial.setNewsId(wmNews.getId());
//            wmNewsMaterial.setType(type);
//            wmNewsMaterialMapper.insert(wmNewsMaterial);
//        }
        wmNewsMaterialMapper.saveRelations(ids, wmNews.getId(), type);
    }

    /**
     * [{type:text,value:文本},{type:image,value:url},{type:image,value:url},{type:image,value:url},{type:image,value:url}]
     * 抽取出 内容中涉及的 素材路径
     *
     * @param content
     */
    private List<String> parseContentImages(String content) {
        // 把内容转换为Map集合
        List<Map> contentMap = JSON.parseArray(content, Map.class);
        // 创建流
        return contentMap.stream()
                // 过滤type为image的数据
                .filter(m -> WemediaConstants.WM_NEWS_TYPE_IMAGE.equals(m.get("type")))
                // 提取每个map中value的值 ==> url路径
                .map(m -> (String) m.get("value"))
                // 替换前缀路径
                .map(url -> url.replaceAll(webSite, ""))
                // 去重
                .distinct()
                // 收集到集合中
                .collect(Collectors.toList());
    }

    /**
     * 保存或修改文章信息
     *
     * @param wmNews
     */
    private void saveOrUpdateWmNews(WmNews wmNews) {
        //先更新时间
        wmNews.setCreatedTime(new Date());
        wmNews.setSubmitedTime(new Date());
        //设置上架
        wmNews.setEnable(WemediaConstants.WM_NEWS_UP);
        if (wmNews.getId() == null) {
            //保存
            save(wmNews);
        } else {
            //修改
            wmNewsMaterialMapper.delete(
                    Wrappers.<WmNewsMaterial>lambdaQuery()
                            .eq(WmNewsMaterial::getNewsId, wmNews.getId())
            );
            updateById(wmNews);
        }

    }

    /**
     * 将图片集合 转为  字符串 按照都好拼接
     *
     * @param images
     * @return
     */
    private String imagesToStr(List<String> images) {
//        if (!CollectionUtils.isEmpty(images)) {
//            return images.stream()
//                    //去除前缀路径
//                    .map(url -> url.replaceAll(webSite, ""))
//                    .collect(Collectors.joining(","));
        if (!CollectionUtils.isEmpty(images)) {
            return images.stream()
                    // 去除前缀路径
                    .map(url -> url.replaceAll(webSite, ""))
                    .collect(Collectors.joining(","));
        }
        return null;
    }
}
